﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace employee.Controls
{
    public partial class emGeneral : common.controls.baseUserControl
    {
        public emGeneral()
        {
            InitializeComponent();

            codeEd.isToUpperCase = true;
            eTagEd.BackColor = common.Settings.myTheme.Info.BackColor;
            this.myToolTip.SetToolTip(this.emImage, "(" + emImage.Width.ToString() + "x" + emImage.Height.ToString() + ")");
            emImage.noImageFilePath = CommonTypes.Settings.sysDefaultImageFile;
        }

        BindingSource lastDataSrc = null;
        public new void Refresh()
        {
            if (this.lastDataSrc != null) SetDataSource(this.lastDataSrc);
        }

        protected virtual void SetDataSource(BindingSource employeeSource)
        {
            common.BindingLibs.BindObject(this.codeEd, employeeSource, DataAccess.DataCache.myBaseDS.employee.emCodeColumn.ColumnName);
            common.BindingLibs.BindObject(this.fullNameEd, employeeSource, DataAccess.DataCache.myBaseDS.employee.fullNameColumn.ColumnName);
            common.BindingLibs.BindObject(this.addressEd1, employeeSource, DataAccess.DataCache.myBaseDS.employee.address1Column.ColumnName);
            common.BindingLibs.BindObject(this.addressEd2, employeeSource, DataAccess.DataCache.myBaseDS.employee.address2Column.ColumnName);
            common.BindingLibs.BindObject(this.phoneEd, employeeSource, DataAccess.DataCache.myBaseDS.employee.phone1Column.ColumnName);
            common.BindingLibs.BindObject(this.mobileEd, employeeSource, DataAccess.DataCache.myBaseDS.employee.mobileColumn.ColumnName);
            common.BindingLibs.BindObject(this.emailEd, employeeSource, DataAccess.DataCache.myBaseDS.employee.email1Column.ColumnName);

            common.BindingLibs.BindObject(this.emImage, employeeSource, DataAccess.DataCache.myBaseDS.employee.emImageColumn.ColumnName,"myImage");
            common.BindingLibs.BindObject(this.sexCb, employeeSource, DataAccess.DataCache.myBaseDS.employee.sexColumn.ColumnName);

            codeEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.emCodeColumn.MaxLength;
            fullNameEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.fullNameColumn.MaxLength;
            addressEd1.MaxLength = DataAccess.DataCache.myBaseDS.employee.address1Column.MaxLength;
            addressEd1.MaxLength = DataAccess.DataCache.myBaseDS.employee.address2Column.MaxLength;
            phoneEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.phone1Column.MaxLength;
            mobileEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.mobileColumn.MaxLength;
            emailEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.email1Column.MaxLength;
        }
        public virtual void Init(BindingSource employeeSource)
        {
            sexCb.LoadData();
            SetDataSource(employeeSource);
            this.lastDataSrc = employeeSource;
        }        
        public virtual bool CheckData()
        {
            bool retVal = true;
            ClearNotifyError();
            if (codeEd.Text.Trim() == "")
            {
                NotifyError(codeLbl);
                retVal = false;
            }
            if (fullNameEd.Text.Trim() == "") 
            {
                NotifyError(fullNameLbl);
                retVal = false;
            }
            //if (sexCb.myValue == null) 
            //{
            //    NotifyError(sexLbl);
            //    retVal = false;
            //}
            if (addressEd1.Text.Trim() == "") 
            {
                NotifyError(addressLbl1);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            state =!state;
            codeEd.Enabled = state;
            fullNameEd.Enabled = state;
            addressEd1.Enabled = state;
            addressEd2.Enabled = state;
            phoneEd.Enabled = state;
            mobileEd.Enabled = state;
            emailEd.Enabled = state;

            eTagEd.Enabled = state;
            eTagScanBtn.Enabled = !state;

            sexCb.Enabled = state;
            imLoadBtn.Enabled = state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            codeEd.Focus();
        }

        private void imageBtn_Click(object sender, EventArgs e)
        {
            try
            {
                bool retVal = true;
                Image img = common.images.GetImage(emImage.myImage, out retVal);
                if (retVal) emImage.myImage = img;
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
    }
}
